﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MobileWebsiteGenerator.Entities;
using System.IO;
using System.Data;

namespace MobileWebsiteGenerator.Controllers
{
    public class PictureGalleryController : BaseController
    {
        //
        // GET: /WebSite/PictureGallery/
        Mobile_appContext context = new Mobile_appContext();

        public ActionResult Index()
        {
            return View(MvcApplication._MainCustomer.PictureGallery);
        }

        public ActionResult Create()
        {


            return View(new pictureGallery());
        }
        [HttpPost]
        public ActionResult Create(pictureGallery picturegallery)
        {
            if (ModelState.IsValid)
            {
                picturegallery.picture_id = MvcApplication._MainCustomer.PictureGallery != null && MvcApplication._MainCustomer.PictureGallery.Count > 0 ? MvcApplication._MainCustomer.PictureGallery.Count() + 1 : 1;
                MvcApplication._MainCustomer.PictureGallery.Add(picturegallery);


                return RedirectToAction("Index", MvcApplication._MainCustomer);
            }

            return View(picturegallery);
        }


        public string Upload(HttpPostedFileBase fileData)
        {

            var fileName = this.Server.MapPath("/Uploads/" + System.IO.Path.GetFileName(fileData.FileName));
            fileData.SaveAs(fileName);

            return Path.GetFileName(fileName);
        }
        //
        // GET: /pictureGallery/Edit/5

        public ActionResult Edit(int id)
        {
            pictureGallery picturegallery = MvcApplication._MainCustomer.PictureGallery.Single(x => x.picture_id == id);
            return View(picturegallery);
        }

        //
        // POST: /pictureGallery/Edit/5

        [HttpPost]
        public ActionResult Edit(pictureGallery picturegallery)
        {
            if (ModelState.IsValid)
            {
                var Prepage = MvcApplication._MainCustomer.PictureGallery.Where(p => p.picture_id == picturegallery.picture_id).FirstOrDefault();
                picturegallery.picture_id = Prepage.picture_id;
                MvcApplication._MainCustomer.PictureGallery.Remove(Prepage);
                MvcApplication._MainCustomer.PictureGallery.Add(picturegallery);
                // context.Entry(picturegallery).State = EntityState.Modified;
                context.SaveChanges();
                return RedirectToAction("index", MvcApplication._MainCustomer);
            }
            return View(picturegallery);
        }

        //
        // GET: /pictureGallery/Delete/5

      
        public string Delete(int id)
        {
            pictureGallery picturegallery = MvcApplication._MainCustomer.PictureGallery.Single(x => x.picture_id == id);
            MvcApplication._MainCustomer.PictureGallery.Remove(picturegallery);
            return "done";
        }

    }
}
